package com.hospital.controller;

import com.hospital.entity.Jurisdiction;
import com.hospital.entity.UserJurisdiction;
import com.hospital.entity.UserJurisdictionVo;
import com.hospital.service.JurisdictionService;
import com.hospital.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限相关controller
 */
@Controller
@CrossOrigin
@RequestMapping("Jurisdiction")
public class JurisdictionController {
    @Autowired
    private JurisdictionService jurisdictionService;
    @Autowired
    private JsonUtil jsonUtil;

    /**
     * 查所有权限
     *
     * @return 集合中的权限信息
     */
    @RequestMapping("SelectAll")
    @ResponseBody
    public List<Jurisdiction> SelectAll() {
        List<Jurisdiction> jurisdictions = jurisdictionService.SelectAll();
        return jurisdictions;
    }

    /**
     * 添加或修改方法
     *
     * @param userId         用户id
     * @param jurisdictionId 权限id
     */
    @RequestMapping("InsertOrUpdate")
    @ResponseBody
    public Boolean InsertOrUpdate(Integer userId, String jurisdictionId) {
        List<Integer> list = new ArrayList<Integer>();
        String[] strs = jurisdictionId.split(",");
        //返回状态
        Integer state = 0;
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            jurisdictionService.BulkDelete(list);
        }
        for (int i = 0; i < strs.length; i++) {
            UserJurisdiction userJurisdiction = new UserJurisdiction(userId, Integer.valueOf(strs[i]));
            state = jurisdictionService.Insert(userJurisdiction);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 查询用户对应权限的信息
     *
     * @param userId 用户id
     * @return 集合中的用户对应权限的信息
     */
    @RequestMapping("SelectByUserJurisdiction")
    @ResponseBody
    public List<UserJurisdictionVo> SelectByUserJurisdiction(Integer userId) {
        List<UserJurisdictionVo> userJurisdictionVos = jurisdictionService.SelectByUserJurisdiction(userId);
        System.out.println(userJurisdictionVos);
        return userJurisdictionVos;
    }
}
